﻿<?php
ob_start();
$dsdv = "";
$chuoitruyvan = "";
include_once($_SERVER['DOCUMENT_ROOT'] . '/htqlmba/configs/database.php');
$madv = $_GET['madv'];
$loaimay = $_GET['loaimay'];
$nhasx = $_GET['nhasx'];
$nsx = $_GET['nsx'];

if ($madv != 'tatca') {// neu don vi khong phai la tat ca
    $db = new database();
    $db->setQuery("select * from don_vi where MA_DON_VI = '" . $madv . "'");
    $re = $db->fetchAll();
    $row = mysql_fetch_array($re);
    $tendv = $row['TEN_DON_VI'];

    //Danh sach may tinh so huu
    $db->setQuery("select * from chi_tiet_so_huu where MA_DON_VI = '" . $madv . "' group by SO_N0");
    $dong = $db->fetchAll();

    $mang = array();
    $i = 0;
    while ($in = mysql_fetch_array($dong)) {
        $mang[$i] = $in['SO_N0'];
        $i++;
    }
    echo " <br >";
    $mang2 = array();
    //Lay danh sach may don vi dang so huu	
    $k = 0;
    for ($j = 0; $j < $i; $j++) {//vong for 
        $db->setQuery("select * from chi_tiet_so_huu where SO_N0 = '" . $mang[$j] . "' order by NGAY_CHUYEN_DEN desc");
        $truyvan = $db->fetchAll();
        $madvtimdc = mysql_fetch_array($truyvan);
        if ($madvtimdc['MA_DON_VI'] == $madv) {
            $mang2[$k] = $madvtimdc['SO_N0'];
            $k++;
        }
    }//ket thuc for 1
    if (count($mang2) == 0) {//kiem tra don vi co may bien ap ko
        echo "<br> Đơn vị này không sở hữu máy biến áp để thống kê";
    } else { //vòng else kiem tra don vi co may bien ap ko
        if ($loaimay != 'tatca') {
            $dem = 0;
            $tennhasx = "Tất cả";
            $tenloai = "";
            $chuoitruyvan = "";
            $chuoitruyvan = "MA_LOAI = '" . $loaimay . "'";
            $db->setQuery("select * from loai_may where MA_LOAI = '" . $loaimay . "'");
            $laytenloai = $db->fetchAll();
            $ketquatenloai = mysql_fetch_array($laytenloai);
            $tenloai = $ketquatenloai['TEN_LOAI'];

            if ($nhasx != 'tatca') {
                // lay ten nha san xuat
                $db->setQuery("select * from nha_sx where MA_NSX = '" . $nhasx . "'");
                $laytennhasx = $db->fetchAll();
                $ketquatennsx = mysql_fetch_array($laytennhasx);
                $tennhasx = $ketquatennsx['TEN_NSX'];
                $chuoitruyvan = $chuoitruyvan . " and MA_NSX = '" . $nhasx . "'";
            }
            if ($nsx != 'tatca')
                $chuoitruyvan = $chuoitruyvan . " and NAM_SX = '" . $nsx . "'";
            else
                $nsx = "Tất cả";

            for ($i = 0; $i < count($mang2); $i++) { //vong for 2
                $db->setQuery("select * from may_bien_ap where SO_N0 = '" . $mang2[$i] . "'  and " . $chuoitruyvan);
                $truyvan1 = $db->fetchAll();
                $ketqua1 = mysql_fetch_array($truyvan1);
                if ($ketqua1['SO_N0'] != NULL)
                    $dem++;
            }//ket thuc for 2


            echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
            echo "<tr>";
            echo "<td colspan ='5' align ='center' height = '30' class='TableHeader1'>Bảng Thống Kê Theo Năm SX, Nhà SX, Loại Máy</td>";

            echo "</tr>";
            echo "<tr>";
            echo "<td width='200'align ='center' class='TableHeaderbc'>Tên Đơn Vị</td>";
            echo "<td width='80'align ='center' class='TableHeaderbc'>Loại Máy</td>";
            echo "<td width='140'align ='center' class='TableHeaderbc'>Nhà Sản Xuất</td>";
            echo "<td width='100'align ='center' class='TableHeaderbc'>Năm Sản Xuất</td>";
            echo "<td width='80'align ='center' class='TableHeaderbc'>Số Lượng</td>";

            echo "</tr>";

            echo "<tr>";
            echo "<td width='200' align ='center' class='TableField2'>" . $tendv . "</td>";
            echo "<td width='80' align ='center' class='TableField2'>" . $tenloai . "</td>";
            echo "<td width='160' align ='center' class='TableField2'>" . $tennhasx . "</td>";
            echo "<td width='100' align ='center' class='TableField2'>" . $nsx . "</td>";
            echo "<td width='80' align ='center' class='TableField2'>" . $dem . "</td>";
            echo "</tr>";

            echo "<tr>";
            echo "<td colspan ='5' align='right'>Tổng số: " . $dem . " máy</td> ";

            echo "</tr>";
            echo "</table>";
        }
        else {//vong else khi tat ca ca loai may
            $tong = 0;
            $chuoitruyvan = "";
            $kiemtra = 0;
            echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
            echo "<tr>";
            echo "<td colspan ='5' align ='center' height = '30' class='TableHeader1'>Bảng Thống Kê Theo Năm SX, Nhà SX, Loại Máy</td>";

            echo "</tr>";
            echo "<tr>";
            echo "<td width='200'align ='center' class='TableHeaderbc'>Tên Đơn Vị</td>";
            echo "<td width='80'align ='center' class='TableHeaderbc'>Loại Máy</td>";
            echo "<td width='140'align ='center' class='TableHeaderbc'>Nhà Sản Xuất</td>";
            echo "<td width='100'align ='center' class='TableHeaderbc'>Năm Sản Xuất</td>";
            echo "<td width='80'align ='center' class='TableHeaderbc'>Số Lượng</td>";

            echo "</tr>";
            echo "<tr>";
            echo "<td width='200' align ='center' class='TableField2'>" . $tendv . "</td>";
            echo "<td  colspan = '4' align ='center'>";
            echo "<table align ='center' border = '0' cellspacing='0' cellpadding='0'>";
            $db->setQuery("select * from loai_may");
            $layloaimay = $db->fetchAll();
            while ($dong = mysql_fetch_array($layloaimay)) {

                $loaimay = $dong['MA_LOAI'];
                $tenloai = $dong['TEN_LOAI'];
                $dem = 0;
                $tennhasx = "Tất cả";
                $chuoitruyvan = "MA_LOAI = '" . $loaimay . "'";
                if ($nhasx != 'tatca') {
                    // lay ten nha san xuat
                    $db->setQuery("select * from nha_sx where MA_NSX = '" . $nhasx . "'");
                    $laytennhasx = $db->fetchAll();
                    $ketquatennsx = mysql_fetch_array($laytennhasx);
                    $tennhasx = $ketquatennsx['TEN_NSX'];
                    $chuoitruyvan = $chuoitruyvan . " and MA_NSX = '" . $nhasx . "'";
                }
                if ($nsx != 'tatca') {
                    $chuoitruyvan = $chuoitruyvan . " and NAM_SX = '" . $nsx . "'";
                    $namsx = $nsx;
                } else
                    $namsx = "Tất cả";

                for ($i = 0; $i < count($mang2); $i++) { //vong for 2
                    $db->setQuery("select * from may_bien_ap where SO_N0 = '" . $mang2[$i] . "'  and " . $chuoitruyvan);
                    $truyvan1 = $db->fetchAll();
                    $ketqua1 = mysql_fetch_array($truyvan1);
                    if ($ketqua1['SO_N0'] != NULL)
                        $dem++;
                }//ket thuc for 2

                $kiemtra = $kiemtra + $dem;
                $tong = $tong + $dem;
                if ($dem != 0) {
                    echo "<tr>";
                    echo "<td width='80' align ='center' class='TableField2'>" . $tenloai . "</td>";
                    echo "<td width='140' align ='center' class='TableField2'>" . $tennhasx . "</td>";
                    echo "<td width='100' align ='center' class='TableField2'>" . $namsx . "</td>";
                    echo "<td width='80' align ='center' class='TableField2'>" . $dem . "</td>";
                    echo "</tr>";
                }
            }
            echo "</table>";
            echo "</td>";
            echo "</tr>";
            if ($kiemtra != 0) {
                echo "<tr>";
                echo "<td colspan ='5' align='right'>Tổng số: " . $tong . " máy</td> ";

                echo "</tr>";
            } else {
                echo "<tr>";
                echo "<td colspan ='5'>Không tìm thấy kết quả thống kê theo các điều kiện đã chọn </td> ";

                echo "</tr>";
            }
            echo "</table>";
        }//vong else khi tat ca ca loai may
    }//vòng else kiem tra don vi co may bien ap ko
}// neu don vi khong phai la tat ca
else {
    $db1 = new database();
    $tongmaycacdv = 0;
    echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
    echo "<tr>";
    echo "<td colspan ='5' align ='center' height = '30' class='TableHeader1'>Bảng Thống Kê Theo Năm SX, Nhà SX, Loại Máy</td>";

    echo "</tr>";
    echo "<tr>";
    echo "<td width='200'align ='center' class='TableHeaderbc'>Tên Đơn Vị</td>";
    echo "<td width='80'align ='center' class='TableHeaderbc'>Loại Máy</td>";
    echo "<td width='140'align ='center' class='TableHeaderbc'>Nhà Sản Xuất</td>";
    echo "<td width='100'align ='center' class='TableHeaderbc'>Năm Sản Xuất</td>";
    echo "<td width='80'align ='center' class='TableHeaderbc'>Số Lượng</td>";
    echo "</tr>";
    $db1->setQuery("select * from don_vi as dv, tai_khoan as tk where dv.MA_DON_VI = tk.MA_DON_VI and  tk.QUYEN = 3");

    $tatcadonvi = $db1->fetchAll();
    while ($dongdv = mysql_fetch_array($tatcadonvi)) {
        $tendv = $dongdv['TEN_DON_VI'];
        $madv = $dongdv['MA_DON_VI'];
        //Danh sach may tinh so huu
        $db1->setQuery("select * from chi_tiet_so_huu where MA_DON_VI = '" . $madv . "' group by SO_N0");
        $dong = $db1->fetchAll();

        $mang = array();
        $i = 0;
        while ($in = mysql_fetch_array($dong)) {
            $mang[$i] = $in['SO_N0'];
            $i++;
        }

        $mang2 = array();
        //Lay danh sach may don vi dang so huu	
        $k = 0;
        for ($j = 0; $j < $i; $j++) {//vong for 1
            $db1->setQuery("select * from chi_tiet_so_huu where SO_N0 = '" . $mang[$j] . "' order by NGAY_CHUYEN_DEN desc");
            $truyvan = $db1->fetchAll();
            $madvtimdc = mysql_fetch_array($truyvan);
            if ($madvtimdc['MA_DON_VI'] == $madv) {
                $mang2[$k] = $madvtimdc['SO_N0'];
                $k++;
            }
        }//ket thuc for 1
        if (count($mang2) == 0) {//kiem tra don vi co so huu may bien ap khong
            $dsdv = $dsdv . ", " . $tendv;
        }//kiem tra don vi co so huu may bien ap khong
        else {// vòng else kiem tra don vi co so huu may bien ap khong
            if ($loaimay != 'tatca') {//if loai may khac tat ca
                $dem = 0;
                $tennhasx = "Tất cả";
                $tenloai = "";
                $namsx = "";
                $chuoitruyvan = "";

                $chuoitruyvan = "MA_LOAI = '" . $loaimay . "'";
                $db1->setQuery("select * from loai_may where MA_LOAI = '" . $loaimay . "'");
                $laytenloai = $db1->fetchAll();
                $ketquatenloai = mysql_fetch_array($laytenloai);
                $tenloai = $ketquatenloai['TEN_LOAI'];

                if ($nhasx != 'tatca') {
                    // lay ten nha san xuat

                    $db1->setQuery("select * from nha_sx where MA_NSX = '" . $nhasx . "'");
                    $laytennhasx = $db1->fetchAll();
                    $ketquatennsx = mysql_fetch_array($laytennhasx);
                    $tennhasx = $ketquatennsx['TEN_NSX'];
                    $chuoitruyvan = $chuoitruyvan . " and MA_NSX = '" . $nhasx . "'";
                }
                if ($nsx != 'tatca') {
                    $chuoitruyvan = $chuoitruyvan . " and NAM_SX = '" . $nsx . "'";
                    $namsx = $nsx;
                } else
                    $namsx = "Tất cả";

                for ($i = 0; $i < count($mang2); $i++) { //vong for 2
                    $db1->setQuery("select * from may_bien_ap where SO_N0 = '" . $mang2[$i] . "'  and " . $chuoitruyvan);
                    $truyvan1 = $db1->fetchAll();

                    $ketqua1 = mysql_fetch_array($truyvan1);
                    if ($ketqua1['SO_N0'] != NULL)
                        $dem++;
                }//ket thuc for 2



                $tongmaycacdv = $tongmaycacdv + $dem;
                echo "<tr>";
                echo "<td width='200' align ='center' class='TableField2'>" . $tendv . "</td>";
                echo "<td width='80' align ='center' class='TableField2'>" . $tenloai . "</td>";
                echo "<td width='160' align ='center' class='TableField2'>" . $tennhasx . "</td>";
                echo "<td width='100' align ='center' class='TableField2'>" . $namsx . "</td>";
                echo "<td width='80' align ='center' class='TableField2'>" . $dem . "</td>";
                echo "</tr>";

                echo "<tr>";
                echo "<td colspan ='5' align='right'>Tổng số: " . $dem . " máy</td> ";

                echo "</tr>";
            }//if loai may khac tat ca
            else {
                $tong = 0;
                $chuoitruyvan = "";
                $kiemtra = 0;

                echo "<tr>";
                echo "<td width='200' align ='center' class='TableField2'>" . $tendv . "</td>";
                echo "<td  colspan = '4' align ='center' class='TableField2'>";
                echo "<table align ='center' border = '0' cellspacing='0' cellpadding='0'>";
                $db1->setQuery("select * from loai_may");
                $layloaimay = $db1->fetchAll();
                while ($dong = mysql_fetch_array($layloaimay)) {

                    $lmay = $dong['MA_LOAI'];
                    $tloai = $dong['TEN_LOAI'];
                    $dem = 0;
                    $tennhasx = "Tất cả";
                    $chuoitruyvan = "MA_LOAI = '" . $lmay . "'";
                    if ($nhasx != 'tatca') {
                        // lay ten nha san xuat
                        $db1->setQuery("select * from nha_sx where MA_NSX = '" . $nhasx . "'");
                        $laytennhasx = $db1->fetchAll();
                        $ketquatennsx = mysql_fetch_array($laytennhasx);
                        $tennhasx = $ketquatennsx['TEN_NSX'];
                        $chuoitruyvan = $chuoitruyvan . " and MA_NSX = '" . $nhasx . "'";
                    }
                    if ($nsx != 'tatca') {
                        $chuoitruyvan = $chuoitruyvan . " and NAM_SX = '" . $nsx . "'";
                        $namsx = $nsx;
                    } else
                        $namsx = "Tất cả";

                    for ($i = 0; $i < count($mang2); $i++) { //vong for 2
                        $db1->setQuery("select * from may_bien_ap where SO_N0 = '" . $mang2[$i] . "'  and " . $chuoitruyvan);
                        $truyvan1 = $db1->fetchAll();
                        $ketqua1 = mysql_fetch_array($truyvan1);
                        if ($ketqua1['SO_N0'] != NULL)
                            $dem++;
                    }//ket thuc for 2

                    $kiemtra = $kiemtra + $dem;
                    $tongmaycacdv = $tongmaycacdv + $dem;
                    $tong = $tong + $dem;
                    if ($dem != 0) {
                        echo "<tr>";
                        echo "<td width='80' align ='center' class='TableField2'>" . $tloai . "</td>";
                        echo "<td width='140' align ='center' class='TableField2'>" . $tennhasx . "</td>";
                        echo "<td width='100' align ='center' class='TableField2'>" . $namsx . "</td>";
                        echo "<td width='80' align ='center' class='TableField2'>" . $dem . "</td>";
                        echo "</tr>";
                    }
                }
                echo "</table>";
                echo "</td>";
                if ($kiemtra != 0) {
                    echo "<tr>";
                    echo "<td colspan ='5' align='right'>Tổng số: " . $tong . " máy</td> ";

                    echo "</tr>";
                } else {
                    echo "<tr>";
                    echo "<td colspan ='5'>Không tìm thấy kết quả thống kê theo các điều kiện đã chọn </td> ";

                    echo "</tr>";
                }
            }
        }//vong else kiem tra don vi co so huu may bien ap khong
    }//end while don vi
    echo "<tr>";
    echo "<td colspan ='5'>  Tổng số tất cả các máy theo điều kiện trên: " . $tongmaycacdv . " máy </td> ";
    echo "</tr>";
    echo "</table>";
    if ($dsdv != "") {
        $dsdv = ltrim($dsdv, ",");
        echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
        echo "<tr>";
        echo "<td width='200' align ='center' class='TableField2'> Danh sách các đơn vị không sở hữu MBA: " . "<br>" . $dsdv . "</td>";
        echo "</tr>";
        echo "</table>";
    }
}
echo "<table align ='center' width='600' border = '0' cellspacing='0' cellpadding='0'>";
echo "<tr>";
echo "<br>";
echo "<td width='600' align ='right'><input class='btn_x' type='submit' name='inexcelsx' id='button' value='Xuất ra File Excel'></td>";
echo "</tr>";
echo "</table>";
?>